package com.briup.shopadmin.service.impl;

import com.briup.shopadmin.common.GlobalException;
import com.briup.shopadmin.common.bean.Admin;
import com.briup.shopadmin.common.utils.JwtUtil;
import com.briup.shopadmin.dao.IAdminDao;
import com.briup.shopadmin.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AdminServiceImpl implements IAdminService {
    @Autowired
    IAdminDao adminDao;

    @Override
    public Map<String, String> login(String username, String password) {
        HashMap<String, String> map = new HashMap<>();
//        判断用户是否存在
        Admin admin = adminDao.findByUsername(username);
        if (admin == null) {
            throw new GlobalException(50004, "用户不存在");
        } else {
//            判断状态是否可用
            if (admin.isStats()) {
//                判断密码是否正确
                if (password.equals(admin.getPassword())) {
//                    生成token
                    String token = JwtUtil.createJWT(username);
                    map.put("token",token);
                    map.put("username",username);
                    return  map;

                }else {
                    throw new GlobalException(50006, "用户名密码错误");
                }

            } else {
                throw new GlobalException(50005, "账号不可用请联系系统管理员");
            }
        }

    }
}
